package com.mobileserver.servlet;

import com.mobileserver.util.DBUtil;
import com.mobileserver.util.RedisUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by xueal on 2017/9/22.
 */
@WebServlet(name = "PrivateChatLoginServlet")
public class PrivateChatLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String nickName = URLDecoder.decode(request.getParameter("nick_name"),"UTF-8");
        String luckNum = URLDecoder.decode(request.getParameter("luck_num"),"UTF-8");
        String relation = URLDecoder.decode(request.getParameter("relation"),"UTF-8");

        Map<String, String> mapUser = new HashMap<String, String>();
        mapUser.put("nick_name", nickName);
        mapUser.put("luck_num", luckNum);
        mapUser.put("relation", relation);

        // 将Map转换为JSONArray数据
        JSONArray jArray = new JSONArray();
        jArray.put(mapUser);
        //定义JSON
        JSONObject jObject=new JSONObject();
        jObject.put("user", jArray);

        String strUserInfo = jObject.toString();

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        Jedis jedis = RedisUtil.getJedis();
//        jedis.set(bookListKey, bookListString);
        jedis.rpush(uuid, strUserInfo);

        PrintWriter out = response.getWriter();
        out.print(uuid);

    }
}
